home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python1.4_Source / Objects / intobject.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  16KB  |  789 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Integer object implementation */
  33.  
  34. #include "allobjects.h"
  35. #include "modsupport.h"
  36.  
  37. #include "protos/intobject_protos.h"
  38.  
  39. #ifdef HAVE_LIMITS_H
  40. #include <limits.h>
  41. #endif
  42.  
  43. #ifndef LONG_MAX
  44. #define LONG_MAX 0X7FFFFFFFL
  45. #endif
  46.  
  47. #ifndef LONG_MIN
  48. #define LONG_MIN (-LONG_MAX-1)
  49. #endif
  50.  
  51. #ifndef CHAR_BIT
  52. #define CHAR_BIT 8
  53. #endif
  54.  
  55. #ifndef LONG_BIT
  56. #define LONG_BIT (CHAR_BIT * sizeof(long))
  57. #endif
  58.  
  59. long
  60. getmaxint Py_PROTO((void))
  61. {
  62.     return LONG_MAX;    /* To initialize sys.maxint */
  63. }
  64.  
  65. /* Standard Booleans */
  66.  
  67. intobject FalseObject = {
  68.     OB_HEAD_INIT(&Inttype)
  69.     0
  70. };
  71.  
  72. intobject TrueObject = {
  73.     OB_HEAD_INIT(&Inttype)
  74.     1
  75. };
  76.  
  77. static object *
  78. err_ovf(msg)
  79.     char *msg;
  80. {
  81.     err_setstr(OverflowError, msg);
  82.     return NULL;
  83. }
  84.  
  85. /* Integers are quite normal objects, to make object handling uniform.
  86.    (Using odd pointers to represent integers would save much space
  87.    but require extra checks for this special case throughout the code.)
  88.    Since, a typical Python program spends much of its time allocating
  89.    and deallocating integers, these operations should be very fast.
  90.    Therefore we use a dedicated allocation scheme with a much lower
  91.    overhead (in space and time) than straight malloc(): a simple
  92.    dedicated free list, filled when necessary with memory from malloc().
  93. */
  94.  
  95. #define BLOCK_SIZE    1000    /* 1K less typical malloc overhead */
  96. #define N_INTOBJECTS    (BLOCK_SIZE / sizeof(intobject))
  97.  
  98. static intobject *
  99. fill_free_list()
  100. {
  101.     intobject *p, *q;
  102.     p = NEW(intobject, N_INTOBJECTS);
  103.     if (p == NULL)
  104.         return (intobject *)err_nomem();
  105.     q = p + N_INTOBJECTS;
  106.     while (--q > p)
  107.         *(intobject **)q = q-1;
  108.     *(intobject **)q = NULL;
  109.     return p + N_INTOBJECTS - 1;
  110. }
  111.  
  112. static intobject *free_list = NULL;
  113. #ifndef NSMALLPOSINTS
  114. #define NSMALLPOSINTS        100
  115. #endif
  116. #ifndef NSMALLNEGINTS
  117. #define NSMALLNEGINTS        1
  118. #endif
  119. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  120. /* References to small integers are saved in this array so that they
  121.    can be shared.
  122.    The integers that are saved are those in the range
  123.    -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
  124. */
  125. static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
  126. #endif
  127. #ifdef COUNT_ALLOCS
  128. int quick_int_allocs, quick_neg_int_allocs;
  129. #endif
  130.  
  131. object *
  132. newintobject(ival)
  133.     long ival;
  134. {
  135.     register intobject *v;
  136. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  137.     if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
  138.         (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
  139.         INCREF(v);
  140. #ifdef COUNT_ALLOCS
  141.         if (ival >= 0)
  142.             quick_int_allocs++;
  143.         else
  144.             quick_neg_int_allocs++;
  145. #endif
  146.         return (object *) v;
  147.     }
  148. #endif
  149.     if (free_list == NULL) {
  150.         if ((free_list = fill_free_list()) == NULL)
  151.             return NULL;
  152.     }
  153.     v = free_list;
  154.     free_list = *(intobject **)free_list;
  155.     v->ob_type = &Inttype;
  156.     v->ob_ival = ival;
  157.     NEWREF(v);
  158. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  159.     if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
  160.         /* save this one for a following allocation */
  161.         INCREF(v);
  162.         small_ints[ival + NSMALLNEGINTS] = v;
  163.     }
  164. #endif
  165.     return (object *) v;
  166. }
  167.  
  168. static void
  169. int_dealloc(v)
  170.     intobject *v;
  171. {
  172.     *(intobject **)v = free_list;
  173.     free_list = v;
  174. }
  175.  
  176. long
  177. getintvalue(op)
  178.     register object *op;
  179. {
  180.     number_methods *nb;
  181.     intobject *io;
  182.     long val;
  183.     
  184.     if (op && is_intobject(op))
  185.         return GETINTVALUE((intobject*) op);
  186.     
  187.     if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
  188.         nb->nb_int == NULL) {
  189.         err_badarg();
  190.         return -1;
  191.     }
  192.     
  193.     io = (intobject*) (*nb->nb_int) (op);
  194.     if (io == NULL)
  195.         return -1;
  196.     if (!is_intobject(io)) {
  197.         err_setstr(TypeError, "nb_int should return int object");
  198.         return -1;
  199.     }
  200.     
  201.     val = GETINTVALUE(io);
  202.     DECREF(io);
  203.     
  204.     return val;
  205. }
  206.  
  207. /* Methods */
  208.  
  209. /* ARGSUSED */
  210. static int
  211. int_print(v, fp, flags)
  212.     intobject *v;
  213.     FILE *fp;
  214.     int flags; /* Not used but required by interface */
  215. {
  216.     fprintf(fp, "%ld", v->ob_ival);
  217.     return 0;
  218. }
  219.  
  220. static object *
  221. int_repr(v)
  222.     intobject *v;
  223. {
  224.     char buf[20];
  225.     sprintf(buf, "%ld", v->ob_ival);
  226.     return newstringobject(buf);
  227. }
  228.  
  229. static int
  230. int_compare(v, w)
  231.     intobject *v, *w;
  232. {
  233.     register long i = v->ob_ival;
  234.     register long j = w->ob_ival;
  235.     return (i < j) ? -1 : (i > j) ? 1 : 0;
  236. }
  237.  
  238. static long
  239. int_hash(v)
  240.     intobject *v;
  241. {
  242.     long x = v -> ob_ival;
  243.     if (x == -1)
  244.         x = -2;
  245.     return x;
  246. }
  247.  
  248. static object *
  249. int_add(v, w)
  250.     intobject *v;
  251.     intobject *w;
  252. {
  253.     register long a, b, x;
  254.     a = v->ob_ival;
  255.     b = w->ob_ival;
  256.     x = a + b;
  257.     if ((x^a) < 0 && (x^b) < 0)
  258.         return err_ovf("integer addition");
  259.     return newintobject(x);
  260. }
  261.  
  262. static object *
  263. int_sub(v, w)
  264.     intobject *v;
  265.     intobject *w;
  266. {
  267.     register long a, b, x;
  268.     a = v->ob_ival;
  269.     b = w->ob_ival;
  270.     x = a - b;
  271.     if ((x^a) < 0 && (x^~b) < 0)
  272.         return err_ovf("integer subtraction");
  273.     return newintobject(x);
  274. }
  275.  
  276. /*
  277. Integer overflow checking used to be done using a double, but on 64
  278. bit machines (where both long and double are 64 bit) this fails
  279. because the double doesn't have enouvg precision.  John Tromp suggests
  280. the following algorithm:
  281.  
  282. Suppose again we normalize a and b to be nonnegative.
  283. Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
  284. Now we test ah and bh against zero and get essentially 3 possible outcomes.
  285.  
  286. 1) both ah and bh > 0 : then report overflow
  287.  
  288. 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out
  289.                         negative
  290.  
  291. 3) ah > 0 and bh = 0  : compute ah*bl and report overflow if it's >= 2^31
  292.                         compute al*bl and report overflow if it's negative
  293.                         add (ah*bl)<<32 to al*bl and report overflow if
  294.                         it's negative
  295.  
  296. In case of no overflow the result is then negated if necessary.
  297.  
  298. The majority of cases will be 2), in which case this method is the same as
  299. what I suggested before. If multiplication is expensive enough, then the
  300. other method is faster on case 3), but also more work to program, so I
  301. guess the above is the preferred solution.
  302.  
  303. */
  304.  
  305. static object *
  306. int_mul(v, w)
  307.     intobject *v;
  308.     intobject *w;
  309. {
  310.     long a, b, ah, bh, x, y;
  311.     int s = 1;
  312.  
  313.     a = v->ob_ival;
  314.     b = w->ob_ival;
  315.     ah = a >> (LONG_BIT/2);
  316.     bh = b >> (LONG_BIT/2);
  317.  
  318.     /* Quick test for common case: two small positive ints */
  319.  
  320.     if (ah == 0 && bh == 0) {
  321.         x = a*b;
  322.         if (x < 0)
  323.             goto bad;
  324.         return newintobject(x);
  325.     }
  326.  
  327.     /* Arrange that a >= b >= 0 */
  328.  
  329.     if (a < 0) {
  330.         a = -a;
  331.         if (a < 0) {
  332.             /* Largest negative */
  333.             if (b == 0 || b == 1) {
  334.                 x = a*b;
  335.                 goto ok;
  336.             }
  337.             else
  338.                 goto bad;
  339.         }
  340.         s = -s;
  341.         ah = a >> (LONG_BIT/2);
  342.     }
  343.     if (b < 0) {
  344.         b = -b;
  345.         if (b < 0) {
  346.             /* Largest negative */
  347.             if (a == 0 || a == 1 && s == 1) {
  348.                 x = a*b;
  349.                 goto ok;
  350.             }
  351.             else
  352.                 goto bad;
  353.         }
  354.         s = -s;
  355.         bh = b >> (LONG_BIT/2);
  356.     }
  357.  
  358.     /* 1) both ah and bh > 0 : then report overflow */
  359.  
  360.     if (ah != 0 && bh != 0)
  361.         goto bad;
  362.  
  363.     /* 2) both ah and bh = 0 : then compute a*b and report
  364.                    overflow if it comes out negative */
  365.  
  366.     if (ah == 0 && bh == 0) {
  367.         x = a*b;
  368.         if (x < 0)
  369.             goto bad;
  370.         return newintobject(x*s);
  371.     }
  372.  
  373.     if (a < b) {
  374.         /* Swap */
  375.         x = a;
  376.         a = b;
  377.         b = x;
  378.         ah = bh;
  379.         /* bh not used beyond this point */
  380.     }
  381.  
  382.     /* 3) ah > 0 and bh = 0  : compute ah*bl and report overflow if
  383.                    it's >= 2^31
  384.                         compute al*bl and report overflow if it's negative
  385.                         add (ah*bl)<<32 to al*bl and report overflow if
  386.                         it's negative
  387.             (NB b == bl in this case, and we make a = al) */
  388.  
  389.     y = ah*b;
  390.     if (y >= (1L << (LONG_BIT/2)))
  391.         goto bad;
  392.     a &= (1L << (LONG_BIT/2)) - 1;
  393.     x = a*b;
  394.     if (x < 0)
  395.         goto bad;
  396.     x += y << LONG_BIT/2;
  397.     if (x < 0)
  398.         goto bad;
  399.  ok:
  400.     return newintobject(x * s);
  401.  
  402.  bad:
  403.     return err_ovf("integer multiplication");
  404. }
  405.  
  406. static int
  407. i_divmod(x, y, p_xdivy, p_xmody)
  408.     register intobject *x, *y;
  409.     long *p_xdivy, *p_xmody;
  410. {
  411.     long xi = x->ob_ival;
  412.     long yi = y->ob_ival;
  413.     long xdivy, xmody;
  414.     
  415.     if (yi == 0) {
  416.         err_setstr(ZeroDivisionError, "integer division or modulo");
  417.         return -1;
  418.     }
  419.     if (yi < 0) {
  420.         if (xi < 0)
  421.             xdivy = -xi / -yi;
  422.         else
  423.             xdivy = - (xi / -yi);
  424.     }
  425.     else {
  426.         if (xi < 0)
  427.             xdivy = - (-xi / yi);
  428.         else
  429.             xdivy = xi / yi;
  430.     }
  431.     xmody = xi - xdivy*yi;
  432.     if (xmody < 0 && yi > 0 || xmody > 0 && yi < 0) {
  433.         xmody += yi;
  434.         xdivy -= 1;
  435.     }
  436.     *p_xdivy = xdivy;
  437.     *p_xmody = xmody;
  438.     return 0;
  439. }
  440.  
  441. static object *
  442. int_div(x, y)
  443.     intobject *x;
  444.     intobject *y;
  445. {
  446.     long d, m;
  447.     if (i_divmod(x, y, &d, &m) < 0)
  448.         return NULL;
  449.     return newintobject(d);
  450. }
  451.  
  452. static object *
  453. int_mod(x, y)
  454.     intobject *x;
  455.     intobject *y;
  456. {
  457.     long d, m;
  458.     if (i_divmod(x, y, &d, &m) < 0)
  459.         return NULL;
  460.     return newintobject(m);
  461. }
  462.  
  463. static object *
  464. int_divmod(x, y)
  465.     intobject *x;
  466.     intobject *y;
  467. {
  468.     long d, m;
  469.     if (i_divmod(x, y, &d, &m) < 0)
  470.         return NULL;
  471.     return mkvalue("(ll)", d, m);
  472. }
  473.  
  474. static object *
  475. int_pow(v, w, z)
  476.     intobject *v;
  477.     intobject *w;
  478.     intobject *z;
  479. {
  480. #if 1
  481.     register long iv, iw, iz, ix, temp, prev;
  482.      int zset = 0;
  483.     iv = v->ob_ival;
  484.     iw = w->ob_ival;
  485.     if (iw < 0) {
  486.         err_setstr(ValueError, "integer to the negative power");
  487.         return NULL;
  488.     }
  489.      if ((object *)z != None) {
  490.         iz = z->ob_ival;
  491.          zset = 1;
  492.     }
  493.     /*
  494.      * XXX: The original exponentiation code stopped looping
  495.      * when temp hit zero; this code will continue onwards
  496.      * unnecessarily, but at least it won't cause any errors.
  497.      * Hopefully the speed improvement from the fast exponentiation
  498.      * will compensate for the slight inefficiency.
  499.      * XXX: Better handling of overflows is desperately needed.
  500.      */
  501.      temp = iv;
  502.     ix = 1;
  503.     while (iw > 0) {
  504.          prev = ix;    /* Save value for overflow check */
  505.          if (iw & 1) {    
  506.              ix = ix*temp;
  507.             if (temp == 0)
  508.                 break; /* Avoid ix / 0 */
  509.             if (ix / temp != prev)
  510.                 return err_ovf("integer pow()");
  511.         }
  512.          iw >>= 1;    /* Shift exponent down by 1 bit */
  513.             if (iw==0) break;
  514.          prev = temp;
  515.          temp *= temp;    /* Square the value of temp */
  516.          if (prev!=0 && temp/prev!=prev)
  517.             return err_ovf("integer pow()");
  518.          if (zset) {
  519.             /* If we did a multiplication, perform a modulo */
  520.              ix = ix % iz;
  521.              temp = temp % iz;
  522.         }
  523.     }
  524.     if (zset) {
  525.          object *t1, *t2;
  526.          long int div, mod;
  527.          t1=newintobject(ix); 
  528.         t2=newintobject(iz);
  529.          if (t1==NULL || t2==NULL ||
  530.              i_divmod((intobject *)t1, (intobject *)t2, &div, &mod)<0) {
  531.              XDECREF(t1);
  532.              XDECREF(t2);
  533.             return(NULL);
  534.         }
  535.         DECREF(t1);
  536.         DECREF(t2);
  537.          ix=mod;
  538.     }
  539.     return newintobject(ix);
  540. #else
  541.     register long iv, iw, ix;
  542.     iv = v->ob_ival;
  543.     iw = w->ob_ival;
  544.     if (iw < 0) {
  545.         err_setstr(ValueError, "integer to the negative power");
  546.         return NULL;
  547.     }
  548.     if ((object *)z != None) {
  549.         err_setstr(TypeError, "pow(int, int, int) not yet supported");
  550.         return NULL;
  551.     }
  552.     ix = 1;
  553.     while (--iw >= 0) {
  554.         long prev = ix;
  555.         ix = ix * iv;
  556.         if (iv == 0)
  557.             break; /* 0 to some power -- avoid ix / 0 */
  558.         if (ix / iv != prev)
  559.             return err_ovf("integer pow()");
  560.     }
  561.     return newintobject(ix);
  562. #endif
  563. }                
  564.  
  565. static object *
  566. int_neg(v)
  567.     intobject *v;
  568. {
  569.     register long a, x;
  570.     a = v->ob_ival;
  571.     x = -a;
  572.     if (a < 0 && x < 0)
  573.         return err_ovf("integer negation");
  574.     return newintobject(x);
  575. }
  576.  
  577. static object *
  578. int_pos(v)
  579.     intobject *v;
  580. {
  581.     INCREF(v);
  582.     return (object *)v;
  583. }
  584.  
  585. static object *
  586. int_abs(v)
  587.     intobject *v;
  588. {
  589.     if (v->ob_ival >= 0)
  590.         return int_pos(v);
  591.     else
  592.         return int_neg(v);
  593. }
  594.  
  595. static int
  596. int_nonzero(v)
  597.     intobject *v;
  598. {
  599.     return v->ob_ival != 0;
  600. }
  601.  
  602. static object *
  603. int_invert(v)
  604.     intobject *v;
  605. {
  606.     return newintobject(~v->ob_ival);
  607. }
  608.  
  609. static object *
  610. int_lshift(v, w)
  611.     intobject *v;
  612.     intobject *w;
  613. {
  614.     register long a, b;
  615.     a = v->ob_ival;
  616.     b = w->ob_ival;
  617.     if (b < 0) {
  618.         err_setstr(ValueError, "negative shift count");
  619.         return NULL;
  620.     }
  621.     if (a == 0 || b == 0) {
  622.         INCREF(v);
  623.         return (object *) v;
  624.     }
  625.     if (b >= LONG_BIT) {
  626.         return newintobject(0L);
  627.     }
  628.     a = (unsigned long)a << b;
  629.     return newintobject(a);
  630. }
  631.  
  632. static object *
  633. int_rshift(v, w)
  634.     intobject *v;
  635.     intobject *w;
  636. {
  637.     register long a, b;
  638.     a = v->ob_ival;
  639.     b = w->ob_ival;
  640.     if (b < 0) {
  641.         err_setstr(ValueError, "negative shift count");
  642.         return NULL;
  643.     }
  644.     if (a == 0 || b == 0) {
  645.         INCREF(v);
  646.         return (object *) v;
  647.     }
  648.     if (b >= LONG_BIT) {
  649.         if (a < 0)
  650.             a = -1;
  651.         else
  652.             a = 0;
  653.     }
  654.     else {
  655.         if (a < 0)
  656.             a = ~( ~(unsigned long)a >> b );
  657.         else
  658.             a = (unsigned long)a >> b;
  659.     }
  660.     return newintobject(a);
  661. }
  662.  
  663. static object *
  664. int_and(v, w)
  665.     intobject *v;
  666.     intobject *w;
  667. {
  668.     register long a, b;
  669.     a = v->ob_ival;
  670.     b = w->ob_ival;
  671.     return newintobject(a & b);
  672. }
  673.  
  674. static object *
  675. int_xor(v, w)
  676.     intobject *v;
  677.     intobject *w;
  678. {
  679.     register long a, b;
  680.     a = v->ob_ival;
  681.     b = w->ob_ival;
  682.     return newintobject(a ^ b);
  683. }
  684.  
  685. static object *
  686. int_or(v, w)
  687.     intobject *v;
  688.     intobject *w;
  689. {
  690.     register long a, b;
  691.     a = v->ob_ival;
  692.     b = w->ob_ival;
  693.     return newintobject(a | b);
  694. }
  695.  
  696. static object *
  697. int_int(v)
  698.     intobject *v;
  699. {
  700.     INCREF(v);
  701.     return (object *)v;
  702. }
  703.  
  704. static object *
  705. int_long(v)
  706.     intobject *v;
  707. {
  708.     return newlongobject((v -> ob_ival));
  709. }
  710.  
  711. static object *
  712. int_float(v)
  713.     intobject *v;
  714. {
  715.     return newfloatobject((double)(v -> ob_ival));
  716. }
  717.  
  718. static object *
  719. int_oct(v)
  720.     intobject *v;
  721. {
  722.     char buf[20];
  723.     long x = v -> ob_ival;
  724.     if (x == 0)
  725.         strcpy(buf, "0");
  726.     else if (x > 0)
  727.         sprintf(buf, "0%lo", x);
  728.     else
  729.         sprintf(buf, "-0%lo", -x);
  730.     return newstringobject(buf);
  731. }
  732.  
  733. static object *
  734. int_hex(v)
  735.     intobject *v;
  736. {
  737.     char buf[20];
  738.     long x = v -> ob_ival;
  739.     if (x >= 0)
  740.         sprintf(buf, "0x%lx", x);
  741.     else
  742.         sprintf(buf, "-0x%lx", -x);
  743.     return newstringobject(buf);
  744. }
  745.  
  746. static number_methods int_as_number = {
  747.     (binaryfunc)int_add, /*nb_add*/
  748.     (binaryfunc)int_sub, /*nb_subtract*/
  749.     (binaryfunc)int_mul, /*nb_multiply*/
  750.     (binaryfunc)int_div, /*nb_divide*/
  751.     (binaryfunc)int_mod, /*nb_remainder*/
  752.     (binaryfunc)int_divmod, /*nb_divmod*/
  753.     (ternaryfunc)int_pow, /*nb_power*/
  754.     (unaryfunc)int_neg, /*nb_negative*/
  755.     (unaryfunc)int_pos, /*nb_positive*/
  756.     (unaryfunc)int_abs, /*nb_absolute*/
  757.     (inquiry)int_nonzero, /*nb_nonzero*/
  758.     (unaryfunc)int_invert, /*nb_invert*/
  759.     (binaryfunc)int_lshift, /*nb_lshift*/
  760.     (binaryfunc)int_rshift, /*nb_rshift*/
  761.     (binaryfunc)int_and, /*nb_and*/
  762.     (binaryfunc)int_xor, /*nb_xor*/
  763.     (binaryfunc)int_or, /*nb_or*/
  764.     0,        /*nb_coerce*/
  765.     (unaryfunc)int_int, /*nb_int*/
  766.     (unaryfunc)int_long, /*nb_long*/
  767.     (unaryfunc)int_float, /*nb_float*/
  768.     (unaryfunc)int_oct, /*nb_oct*/
  769.     (unaryfunc)int_hex, /*nb_hex*/
  770. };
  771.  
  772. typeobject Inttype = {
  773.     OB_HEAD_INIT(&Typetype)
  774.     0,
  775.     "int",
  776.     sizeof(intobject),
  777.     0,
  778.     (destructor)int_dealloc, /*tp_dealloc*/
  779.     (printfunc)int_print, /*tp_print*/
  780.     0,        /*tp_getattr*/
  781.     0,        /*tp_setattr*/
  782.     (cmpfunc)int_compare, /*tp_compare*/
  783.     (reprfunc)int_repr, /*tp_repr*/
  784.     &int_as_number,    /*tp_as_number*/
  785.     0,        /*tp_as_sequence*/
  786.     0,        /*tp_as_mapping*/
  787.     (hashfunc)int_hash, /*tp_hash*/
  788. };
  789.